/**
 * Title:        字段属性检测
 * Company:      泛微软件
 */


package checkDetail;

import general.BaseBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

import weaver.conn.RecordSet;
import weaver.general.Util;



public class BillfieldAndNodeflowCheck extends BaseBean implements CheckBase  {

    @Override
    public JSONObject checkData(HashMap dataMap) {
        setRecordSet();
        (new BaseBean()).writeLog("BillfieldAndNodeflowCheck  start");

        List resultList = new ArrayList();
        String result = "";
        recordSet = getDataCount();
        int cnt = recordSet.getCounts();
        if(cnt > 0){
            result = resultNeedFix;
            Map resultMap = new HashMap<String,String>();
            resultMap.put("showMessage", cnt + "条流程存在问题，需要修复。");
            resultMap.put("buttonname","修复数据");
            resultList.add(resultMap);
            
        }else{
            result = resultOk;
        }
        (new BaseBean()).writeLog("BillfieldAndNodeflowCheck  end");
        return formatReturnJsonCheck(result, resultList,this);
    }

    @Override
    public JSONObject fixData(HashMap dataMap) {
        setRecordSet();

        List resultList = new ArrayList();
        String result = "";
        if(dataMap!= null && dataMap.get("divid")!= null){
            //根据参数获取列表
            String divid =  (String)dataMap.get("divid");

            fixdataDetail();

            int cnt = getDataCount().getCounts();
            if(cnt > 0){
                result = resultNeedFix;
                Map resultMap = new HashMap<String,String>();
                resultMap.put("fixMessage","修复失败");
                resultMap.put("divid",divid);
                resultMap.put(result,resultNeedFix);
                resultList.add(resultMap);
            }else{
                result = resultOk;
                Map resultMap = new HashMap<String,String>();
                resultMap.put("fixMessage","修复成功");
                resultMap.put("divid",divid);
                resultMap.put(result,resultOk);
                resultList.add(resultMap);
            }
            return formatReturnJsonFix(result, resultList,this);
        }else{
            fixdataDetail();
            return checkData(dataMap);
        }
        
        
    }

    @Override
    public String getClassName() {
        return "nodeform数据一致性检测";
    }

    @Override
    public String getClassDesc() {
        return "表workflow_billfield与workflow_nodeform表数据一致性检测修复";
    }

    private RecordSet getDataCount(){

        String sql = "";

        sql += " select distinct d.id as workflowid ";
        sql += "   from workflow_nodeform a ";
        sql += "   left join workflow_flownode c ";
        sql += "     on a.nodeid = c.nodeid ";
        sql += "   left join workflow_base d ";
        sql += "     on c.workflowid = d.id ";
        sql += "  left join workflow_nodebase ";
        sql += "     on a.nodeid = workflow_nodebase.id ";
        sql += "  where not exists (select * from workflow_billfield b where a.fieldid = b.id) ";
        sql += "    and d.isbill = 1 ";
        sql += "    and d.formid < 0 ";
        sql += "    and a.fieldid > 0 ";
        sql += "    and (a.isview = '1' ";
        sql += "    or a.isedit = '1' ";
        sql += "    or a.ismandatory = '1') ";
        recordSet.executeSql(sql);
        return recordSet;
    }
    private void fixdataDetail(){

        String sql = "";
        sql += "select a.*, ";
        sql += "       d.id as workflowid, ";
        sql += "       d.workflowname, ";
        sql += "       workflow_nodebase.nodename ";
        sql += "  from workflow_nodeform a ";
        sql += "  left join workflow_flownode c ";
        sql += "    on a.nodeid = c.nodeid ";
        sql += "  left join workflow_base d ";
        sql += "    on c.workflowid = d.id ";
        sql += " left join workflow_nodebase ";
        sql += "    on a.nodeid = workflow_nodebase.id ";
        sql += " where not exists (select * from workflow_billfield b where a.fieldid = b.id) ";
        sql += "   and d.isbill = 1 ";
        sql += "   and d.formid < 0 ";
        sql += "   and a.fieldid > 0 ";
        sql += "   and (a.isview = '1' ";
        sql += "   or a.isedit = '1' ";
        sql += "   or a.ismandatory = '1') order by d.id ,a.nodeid,a.fieldid ";
        
        recordSet.executeSql(sql);
        RecordSet rs = new RecordSet();
        while(recordSet.next()){
            int nodeid = recordSet.getInt("nodeid");
            int fieldid = recordSet.getInt("fieldid");
            //可编辑属性为空
            String strsql = "update workflow_nodeform set isedit ='0',isview ='0',ismandatory ='0'  where nodeid=" + nodeid + " and fieldid=" + fieldid ;
            rs.executeSql(strsql);
        }
    }
}



